<?php
	//List of lotteries
	
//Debugger::VarDump(HTTPContext::$session);
	if (HTTPContext::$session['success'] == '1')
	{
		//message
		HtmlsHelper::Message(array("text" => "You have successfully bought tickets!"));
		//unset(HTTPContext::$session['success']);
	}
	
	if (HTTPContext::$session['success'] == '0')
	{
		//message
		HtmlsHelper::Message(array("text" => "Tickets was not bought!"));
		//unset(HTTPContext::$session['success']);
	}

	echo "
		<div class='pageHeading'>" . Language::Get('Your Lotteries') . "</div>
		<table cellspacing='0' cellpadding='4' border='0' width='100%' class='messageList'>
		<tr>
			<th>". Language::Get('Lottery Type') . "</th>
			<th>". Language::Get('lottery start') . "</th>
			<th>". Language::Get('lottery end') . "</th>
			<th>". Language::Get('Money gained') . "</th>
		</tr>";
	
	if (count ($this->data[$this->name]) == 0)
	{
		echo "<tr><td colspan=4>No records</td></tr>"; 
	}
	
	foreach ($this->data[$this->name] as $key => $value)
	{
		$datetimeStart = explode(' ', $value['start_datetime']);
		$datetimeEnd = explode(' ', $value['end_datetime']);
		//Debugger::VarDump($value['start_datetime']);
		echo "<tr>
				<td>" . $value['type_name'] . "</td>
				<td>" . $datetimeStart[0] .", ". $datetimeStart[1] ." ". $datetimeStart[2] . "</td>
				<td>" . ($datetimeEnd[0] == '' ? "Running..." : ($datetimeEnd[0] .", ". $datetimeEnd[1] ." ". $datetimeEnd[2])) . "</td>
				<td>" . ($datetimeEnd[0] == '' ? "-" : ($value['client_id'] == HTTPContext::$session['client']['id'] ? $value['type_name'] : "0") ) . "</td>
			</tr>";		
	}
		
	echo "</table>";
	
	//pagination
	if (count ($this->data[$this->name]) != 0)
	{
		echo "<div style = 'padding: 15px; '>".HtmlsHelper::Pagination($this->data['num_rows'])."</div>";
	}
	
	//New lottery
	echo "
		<div class='pageHeading'>" . Language::Get('New Lottery') . "</div>";
	XHTML::Form('/clientslotteries/create/', FormMethod::Post, array('id' => 'lotteryForm'));
	XHTML::InputHidden('client_id', HTTPContext::$session['client']['id']);
	XHTML::InputHidden('sum', 0, 'id="sum"');
	echo "
        <table cellspacing='0' cellpadding='0' border='0' style = 'margin-top: 15px;'>
			<tr>
				<td width = 150px>" . Language::Get('Lottery type name') . ":</td>
				<td class='inputType-1-left'></td>
				<td class='inputType-1-center'>";
					XHTML::Select('lottery_type_id', $this->data['LotteryTypes'], '', false, array('style' => 'border: 0px; width: 235px;', 'onchange' => 'TypeChange();', 'id' => 'lotteryType'));
	echo "
				</td>
				<td class='inputType-1-right'></td>
			</tr>
			<tr><td colspan=4 style='height:7px;'></td></tr>";
	// number of tickets
	echo "	<tr>
				<td width = 150px>" . Language::Get('Tickets num') . " (<span id='ticket_price'>10</span>$ per ticket):</td>
				<td class='inputType-1-left'></td>
				<td class='inputType-1-center'>";
					XHTML::InputText('tickets_num', 1, 'id="ticketsNum" onchange="TicketsNumChange();"');
	echo "
				</td>
				<td class='inputType-1-right'></td>
			</tr>
				<td width=100>&nbsp;</td>
					
			</tr>";
	//total
	echo "	<tr>
				<td>
					Total:
				</td>
				<td colspan=5>
					<span id='total'>5</span>$
				</td>
			</tr>
				
		</table>
		
		<table cellspacing='0' cellpadding='0' border='0' style = 'margin-top: 15px;'>
			<tr>
				<td class='btnType-5-left btnType-5-left-select'></td>
				<td class='btnType-5-center btnType-5-center-select' align='center' width=90px>
					<span class='btnType-5-txt cpointer' onclick='BeforeSubmit()'>".Language::Get('Get it!')."</span>
				</td>
				<td class='btnType-5-right btnType-5-right-select'></td>
			</tr>
		</table>
	</form>";

?>

<script type="text/javascript">

	var lotteryTypes = [];
	var ticketPrice = 10;
	var ticketsNum = 1;
	
	var money = <?php echo $this->data['Client']['sum']; ?>;
	
	<?php 
		foreach ($this->data['LotteryTypes'] as $key => $value)
		{
			echo "lotteryTypes[" . $value['id'] . "] = " . $value['ticket_price'] . ";";
		}
	?>

	function TypeChange()
	{
		ticketPrice = lotteryTypes[$('#lotteryType').val()];
		ReCalc();
	}

	function TicketsNumChange()
	{
		ticketsNum = parseInt($('#ticketsNum').val(), 10);
		if (isNum($('#ticketsNum').val()) == null || ticketsNum < 1)
		{
			alert('Wrong tickets number!');
			ticketsNum = 1;
			$('#ticketsNum').val('1');
		}
		ReCalc();
	}

	function ReCalc()
	{
		$('#ticket_price').html(ticketPrice);
		$('#total').html(ticketPrice * ticketsNum);
		$('#sum').val(ticketPrice * ticketsNum);
	}

	function BeforeSubmit()
	{
		if (parseInt($('#total').html()) > money)
		{
			alert("Not enough money!");
			return false;
		}
		if (isNaN(ticketsNum) || ticketsNum < 1)
		{
			alert('Wrong tickets number!');
			return false;
		}
		$('#lotteryForm').submit();
		return false;
	}

	function isNum(string)
	{
		//var numericExpression = /^[0-9]+$/;
		return string.match(/^[0-9]+$/); 

	}
	
</script>